package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjNetworkEmpBand;
import com.zb.mapper.ZjNetworkEmpBandMapper;
import com.zb.mobile.NetworkEmpBandListMobileVo;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkEmpBandService;
import com.zb.vo.NetworkEmpBandSelectVo;
import com.zb.vo.NetworkEmpBandVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网点人员银行卡表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-05-24
 */
@Service
public class ZjNetworkEmpBandServiceImpl extends ServiceImpl<ZjNetworkEmpBandMapper, ZjNetworkEmpBand> implements ZjNetworkEmpBandService {
    @Resource
    private ZjNetworkEmpBandMapper networkEmpBandMapper;

    // 保存添加银行卡号
    @Override
    public void save(AccounLoginInfo loginInfo, NetworkEmpBandVo empBandVo) {
        Assert.isNull(networkEmpBandMapper.existByCode(empBandVo.getId(), loginInfo.getNetworkEmpId(), empBandVo.getCode()), "当前银行卡已添加：" + empBandVo.getCode());
        ZjNetworkEmpBand empBand = new ZjNetworkEmpBand();
        BeanUtil.copyProperties(empBandVo, empBand);
        empBand.setOpeningBank(null == empBandVo.getOpeningBank()? "" : empBandVo.getOpeningBank());
        if (null != empBand.getId()) {
            this.updateById(empBand);
            return;
        }
        empBand.setId(IdWorker.getId());
        empBand.setNetworkEmpId(loginInfo.getNetworkEmpId());
        this.save(empBand);
    }

    // 获取银行卡列表
    @Override
    public Map<String, Object> getBandList(AccounLoginInfo loginInfo, PageUtil pageUtil) {
        PageUtil.setPageLimitMobile(pageUtil);
        QueryWrapper<ZjNetworkEmpBand> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "type", "code")
                .eq("network_emp_id", loginInfo.getNetworkEmpId());
        Page<ZjNetworkEmpBand> page = this.page(new Page<>(pageUtil.getPage(), pageUtil.getLimit()), wrapper);
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords().stream().map(band -> {
            NetworkEmpBandListMobileVo vo = new NetworkEmpBandListMobileVo();
            BeanUtil.copyProperties(band, vo);
            vo.setCode("···· ···· ···· " + band.getCode().substring(band.getCode().length()-4));
            return vo;
        }).toList());
    }

    // 获取银行卡详情
    @Override
    public NetworkEmpBandVo getBandDetail(Long id) {
        ZjNetworkEmpBand empBand = this.getById(id);
        Assert.notNull(empBand, "没有此银行卡信息：" + id);
        NetworkEmpBandVo vo = new NetworkEmpBandVo();
        BeanUtil.copyProperties(empBand, vo);
        return vo;
    }

    // 获取银行卡下拉数据
    @Override
    public List<NetworkEmpBandSelectVo> getNetworkEmpBandSelect(Long networkEmpId) {
        QueryWrapper<ZjNetworkEmpBand> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "code", "opening_bank", "cardholder_name","open_account_bank_network","bank_code")
                .eq("network_emp_id", networkEmpId);
        return this.list(wrapper).stream().map(band -> {
            NetworkEmpBandSelectVo vo = new NetworkEmpBandSelectVo();
            BeanUtil.copyProperties(band, vo);
            return vo;
        }).toList();
    }
}
